<?php

/**
 * Description of ServerUrl
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class ServerUrl extends ActiveRecord {

    /**
     * The table name.
     */
    const TABLE = 'server_url';

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
    }

    /**
     * Getting the location of the www files or resource files for this host.
     *
     * @param int $subdomain
     * @param string $folder
     * @return string
     */
    public static function getHost($subdomain, $folder = 'www') {
        $apcKey = static::TABLE . $folder . '_' . $subdomain;
        $params = array('table' => static::TABLE, 'folder' => $folder, 'subdomain' => $subdomain);
        $callback = function($params) {
            $query = new Query('
                SELECT :folder
                FROM :table
                WHERE `subdomain` = :subdomain
                AND `server_type` = :server_type;
            ');
            $query->bindFieldName('folder', $params['folder']);
            $query->bindFieldName('table', $params['table']);
            $query->bindVariable('subdomain', $params['subdomain']);
            $query->bindVariable('server_type', SERVER);
            return System::getDB()->queryArray($query, TRUE);
        };        
        $fields = Utils::apcCache($apcKey, $callback, STORED_AS_CACHE == YES, $params);        
        
        return $fields[$folder];
    }

}